package com.zhaoyan.aopdemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * Created by 赵岩 on 2018/11/30.
 */
@Aspect
public class AspectJTest {
    private static final String TAG = "tag00";

    @Pointcut("execution(@ com.zhaoyan.aopdemo.AspectJAnnotation  * *(..))")
    public void executionAspectJ() {
    }

    @Around("executionAspectJ()")
    public Object aroundAspectJ(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Log.i(TAG, "aroundAspectJ(ProceedingJoinPoint joinPoint)");
        AspectJAnnotation aspectJAnnotation = methodSignature.getMethod().getAnnotation(AspectJAnnotation.class);
        String permission = aspectJAnnotation.value();
        Context aThis = (Context) joinPoint.getThis();
        if (permission.equals("权限A")) {
            Object result = joinPoint.proceed();
            Log.i(TAG, "有权限：" + permission);
            return result;
        } else {
            Toast.makeText(aThis, "没有权限", Toast.LENGTH_LONG).show();
//            ((MainActivity) aThis).finish();
//            if (aThis instanceof MainActivity) {
//                Intent intent = new Intent(aThis, Main2Activity.class);
//                aThis.startActivity(intent);
//            }
        }
        return "";
    }
}
